<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. AsyncStorage ######1). AsyncStorageAsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统，它对于App来说是全局性的。它用来代替LocalStorage。 ######2). 存储位置存储在应用包目录下的databases文件夹下，类似于SharedPreferences">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="React-Native-数据存储">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/React-Native/React-Native-数据存储/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. AsyncStorage ######1). AsyncStorageAsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统，它对于App来说是全局性的。它用来代替LocalStorage。 ######2). 存储位置存储在应用包目录下的databases文件夹下，类似于SharedPreferences">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f0ae664b06936851.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:29:59.138Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="React-Native-数据存储">
<meta name="twitter:description" content="####1. AsyncStorage ######1). AsyncStorageAsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统，它对于App来说是全局性的。它用来代替LocalStorage。 ######2). 存储位置存储在应用包目录下的databases文件夹下，类似于SharedPreferences">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-f0ae664b06936851.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/React-Native/React-Native-数据存储/"/>





  <title>React-Native-数据存储 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/React-Native/React-Native-数据存储/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">React-Native-数据存储</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. AsyncStorage</p>
<p>######1). AsyncStorage<br>AsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统，它对于App来说是全局性的。它用来代替LocalStorage。</p>
<p>######2). 存储位置<br>存储在应用包目录下的databases文件夹下，类似于SharedPreferences<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f0ae664b06936851.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><a id="more"></a></p>
<p>######3). 示例代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123;PureComponent&#125; from &apos;react&apos;;</span><br><span class="line">import &#123;</span><br><span class="line">	Button,</span><br><span class="line">	StyleSheet,</span><br><span class="line">	View,</span><br><span class="line">	AsyncStorage</span><br><span class="line">&#125; from &apos;react-native&apos;;</span><br><span class="line"></span><br><span class="line">// KEY</span><br><span class="line">let STORAGE_KEY = &apos;TEST&apos;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @FileName: AsyncStorageDemo</span><br><span class="line"> * @Author: mazaiting</span><br><span class="line"> * @Date: 2018/6/20</span><br><span class="line"> * @Description:</span><br><span class="line"> */</span><br><span class="line">class AsyncStorageDemo extends PureComponent &#123;</span><br><span class="line">	render() &#123;</span><br><span class="line">		return (</span><br><span class="line">			&lt;View style=&#123;styles.container&#125;&gt;</span><br><span class="line">				&lt;Button</span><br><span class="line">					title=&#123;&apos;保存数据&apos;&#125;</span><br><span class="line">					onPress=&#123;() =&gt; this.saveData()&#125;</span><br><span class="line">					style=&#123;&#123;</span><br><span class="line">						marginTop: 10</span><br><span class="line">					&#125;&#125;</span><br><span class="line">				/&gt;</span><br><span class="line">				&lt;Button</span><br><span class="line">					title=&#123;&apos;删除数据&apos;&#125;</span><br><span class="line">					onPress=&#123;() =&gt; this.removeData()&#125;</span><br><span class="line">					style=&#123;&#123;</span><br><span class="line">						marginTop: 10</span><br><span class="line">					&#125;&#125;</span><br><span class="line">				/&gt;</span><br><span class="line">				&lt;Button</span><br><span class="line">					title=&#123;&apos;获取数据&apos;&#125;</span><br><span class="line">					onPress=&#123;() =&gt; this.getData()&#125;</span><br><span class="line">					style=&#123;&#123;</span><br><span class="line">						marginTop: 10</span><br><span class="line">					&#125;&#125;</span><br><span class="line">				/&gt;</span><br><span class="line">			&lt;/View&gt;</span><br><span class="line">		)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 保存数据</span><br><span class="line">	 */</span><br><span class="line">	async saveData() &#123;</span><br><span class="line">		alert(&quot;saveData&quot;)</span><br><span class="line">		await AsyncStorage</span><br><span class="line">			.setItem(STORAGE_KEY, &apos;mazaiting&apos;)</span><br><span class="line">			.then(value =&gt; &#123;</span><br><span class="line">				alert(&quot;存储成功：&quot; + value)</span><br><span class="line">			&#125;)</span><br><span class="line">			.catch(reason =&gt; &#123;</span><br><span class="line">				alert(reason.message)</span><br><span class="line">			&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 删除数据</span><br><span class="line">	 */</span><br><span class="line">	async removeData() &#123;</span><br><span class="line">		await AsyncStorage</span><br><span class="line">			.removeItem(STORAGE_KEY)</span><br><span class="line">			.then(value =&gt; &#123;</span><br><span class="line">				alert(&quot;移除成功：&quot; + value)</span><br><span class="line">			&#125;)</span><br><span class="line">			.catch(reason =&gt; &#123;</span><br><span class="line">				alert(reason.message)</span><br><span class="line">			&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 获取数据</span><br><span class="line">	 */</span><br><span class="line">	async getData() &#123;</span><br><span class="line">		await AsyncStorage</span><br><span class="line">			.getItem(STORAGE_KEY)</span><br><span class="line">			.then(value =&gt; &#123;</span><br><span class="line">				alert(&quot;存储的值&quot; + value)</span><br><span class="line">			&#125;)</span><br><span class="line">			.catch(reason =&gt; &#123;</span><br><span class="line">				alert(reason.message)</span><br><span class="line">			&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 样式属性</span><br><span class="line"> */</span><br><span class="line">const styles = StyleSheet.create(&#123;</span><br><span class="line">	container: &#123;</span><br><span class="line">		flex: 1,</span><br><span class="line">		backgroundColor: &apos;#DDD&apos;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 导出当前Module</span><br><span class="line"> */</span><br><span class="line">module.exports = AsyncStorageDemo;</span><br></pre></td></tr></table></figure></p>
<p>######4). 回调方法<br>ES6中promise提供了几个回调方法then，done，finally，如下所示：</p>
<ul>
<li>then()方法会在setItem开始执行后执行</li>
<li>done()方法会在setItem执行完成后调用，done<br>都会捕捉到任何可能出现的错误，并向全局抛出</li>
<li>catch()是捕获异常数据的</li>
<li>finally()是回调链执行的最后一个方法<br>####2. <a href="https://github.com/sunnylqm/react-native-storage" target="_blank" rel="noopener">react-native-storage</a><br>######1). 安装<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install react-native-storage --save</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######2). 配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">import React from &apos;react&apos;;</span><br><span class="line">import &#123;AsyncStorage&#125; from &apos;react-native&apos;;</span><br><span class="line">// npm install react-native-storage --save</span><br><span class="line">import Storage from &apos;react-native-storage&apos;;</span><br><span class="line"></span><br><span class="line">const storage = new Storage(&#123;</span><br><span class="line">	// 容量</span><br><span class="line">	size : 1000,</span><br><span class="line">	// RN 使用 AsyncStorage</span><br><span class="line">	storageBackend : AsyncStorage,</span><br><span class="line">	// 有效时间</span><br><span class="line">	defaultExpires: 1000 * 3600 * 24,</span><br><span class="line">	// 缓存数据在内存中</span><br><span class="line">	enableCache: true,</span><br><span class="line">	// 数据过期时调用</span><br><span class="line">	sync: &#123;</span><br><span class="line">	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">// RN</span><br><span class="line">global.storage = storage;</span><br><span class="line">//在这里设置`storage.sync`</span><br><span class="line">// storage.sync = require(&apos;./RNAsyncStorage_asyn&apos;).xxxx;</span><br></pre></td></tr></table></figure></p>
<p>######3). 简单使用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123;PureComponent&#125; from &apos;react&apos;;</span><br><span class="line">import &#123;</span><br><span class="line">	Button,</span><br><span class="line">	StyleSheet,</span><br><span class="line">	View</span><br><span class="line">&#125; from &apos;react-native&apos;;</span><br><span class="line">// 初始化全局storage</span><br><span class="line">import &apos;./Storage&apos;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @FileName: RnStorageDemo</span><br><span class="line"> * @Author: mazaiting</span><br><span class="line"> * @Date: 2018/6/20</span><br><span class="line"> * @Description:</span><br><span class="line"> */</span><br><span class="line">class RnStorageDemo extends PureComponent &#123;</span><br><span class="line">	render() &#123;</span><br><span class="line">		return (</span><br><span class="line">			&lt;View style=&#123;styles.container&#125;&gt;</span><br><span class="line">				&lt;View&gt;</span><br><span class="line">					&lt;Button</span><br><span class="line">						title=&#123;&apos;简单存储&apos;&#125;</span><br><span class="line">						onPress=&#123;() =&gt; this.simpleSaveData()&#125;</span><br><span class="line">					/&gt;</span><br><span class="line">					&lt;Button</span><br><span class="line">						title=&#123;&apos;简单读取&apos;&#125;</span><br><span class="line">						onPress=&#123;() =&gt; this.simpleGetData()&#125;</span><br><span class="line">					/&gt;</span><br><span class="line">				&lt;/View&gt;</span><br><span class="line">			&lt;/View&gt;</span><br><span class="line">		)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 简单存储</span><br><span class="line">	 */</span><br><span class="line">	simpleSaveData() &#123;</span><br><span class="line">		storage.save(&#123;</span><br><span class="line">			key: &apos;loginState&apos;,</span><br><span class="line">			data: &#123;</span><br><span class="line">				state: true</span><br><span class="line">			&#125;,</span><br><span class="line">			// expires 为有效时间</span><br><span class="line">			expires: 1000 * 3600</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 简单读取</span><br><span class="line">	 */</span><br><span class="line">	simpleGetData() &#123;</span><br><span class="line">		storage.load(&#123;</span><br><span class="line">			key: &apos;loginState&apos;,</span><br><span class="line">			// autoSync(默认为true)意味着在没有找到数据或数据过期时自动调用相应的sync方法</span><br><span class="line">			autoSync: true,</span><br><span class="line">			// syncInBackground(默认为true)意味着如果数据过期，</span><br><span class="line">			// 在调用sync方法的同时先返回已经过期的数据。</span><br><span class="line">			// 设置为false的话，则等待sync方法提供的最新数据(当然会需要更多时间)。</span><br><span class="line">			syncInBackground: true,</span><br><span class="line">			// 给sync方法传递额外的参数</span><br><span class="line">			syncParams: &#123;</span><br><span class="line">				extraFetchOptions: &#123;</span><br><span class="line">					// 各种参数</span><br><span class="line">				&#125;,</span><br><span class="line">				someFlag: true</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;).then(value =&gt; &#123;</span><br><span class="line">			// 如果找到数据，则在then方法中返回</span><br><span class="line">			console.log(value.state);</span><br><span class="line">			alert(value.state)</span><br><span class="line">		&#125;).catch(reason =&gt; &#123;</span><br><span class="line">			// 异常</span><br><span class="line">			console.error(reason.message)</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 样式属性</span><br><span class="line"> */</span><br><span class="line">const styles = StyleSheet.create(&#123;</span><br><span class="line">	container: &#123;</span><br><span class="line">		backgroundColor: &apos;#DDD&apos;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 导出当前Module</span><br><span class="line"> */</span><br><span class="line">module.exports = RnStorageDemo;</span><br></pre></td></tr></table></figure></p>
<p>####3. <a href="https://github.com/andpor/react-native-sqlite-storage" target="_blank" rel="noopener">react-native-sqlite-storage</a></p>
<p>######1). SQLite3 Native Plugin for React Native for both Android (Classic and Native) and iOS</p>
<p>######2). 安装<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save react-native-sqlite-storage</span><br></pre></td></tr></table></figure></p>
<p>######3). 配置</p>
<ul>
<li><p>修改android项目的settings.gradle</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">include &apos;:react-native-sqlite-storage&apos;</span><br><span class="line">project(&apos;:react-native-sqlite-storage&apos;).projectDir = new File(rootProject.projectDir, &apos;../node_modules/react-native-sqlite-storage/src/android&apos;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>修改app\build.gradle</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    ...</span><br><span class="line">    compile project(&apos;:react-native-sqlite-storage&apos;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>修改MainApplication.java，添加SQLitePluginPackage</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">protected List&lt;ReactPackage&gt; getPackages() &#123;</span><br><span class="line">    return Arrays.&lt;ReactPackage&gt;asList(</span><br><span class="line">            new MainReactPackage(),</span><br><span class="line">            new SQLitePluginPackage()</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/React-Native/React-Native-TabNavigator底部导航/" rel="next" title="React-Native-TabNavigator底部导航">
                <i class="fa fa-chevron-left"></i> React-Native-TabNavigator底部导航
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/React-Native/React-Native-离线包-图片资源/" rel="prev" title="React-Native-离线包-图片资源">
                React-Native-离线包-图片资源 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
